<?php
class TypeModel extends Model{
	
	public $table = 'type';
	
	public $validate = array(
		array('typename',0,'类型名称不能为空',2,3)
	); 
	//添加商品类型
	public function add_type(){
		if(!$this->create()) return false;
		$typename = Q('post.typename');
		if($this->get_one_type(array('typename'=>$typename))){
			$this->error = $typename . '类型已存在';
			return false;
		}
		return $this->add();
				
	}
	//获得所有类型
	public function get_all_type($where=Null,$needPage=FALSE){
		if($needPage){
		//分页类用法
		//总条数
		$total=$this->where($where)->count();
		//2表示每页取多少条，3表示页码的数量
		$pageObj = new Page($total,10,3);
		//显示分页，与验证码code一样，用show();
		$page = $pageObj->show();
		//分页类算出limit
		$limit = $pageObj->limit();
		//在使用分页类的时候一定要注意limit
		$data = $this->where($where)->limit($limit)->all();
		$data['page'] = $page;
		}else{
			$data = $this->where($where)->all();
		}
		return $data;
	}
	//获得一条类型名
	public function get_one_type($where=Null,$field=Null){
		return $this->where($where)->field($field)->find();
	}
	//类型编辑
	public function edit_type($where=Null){
//		p($_POST);die;
		return $this->where($where)->save();
	}
	//类型删除
	public function delete_type($where){
//		p($_POST);die;
		$this->where($where)->delete();

	}
	
	
	
	
}
